package cn.tedu.oa.sys.service.impl;


import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.sys.dao.LogDao;
import cn.tedu.oa.sys.entity.Log;
import cn.tedu.oa.sys.service.LogService;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LogServiceImpl extends BaseServiceImpl<Log> implements LogService {

    @Autowired
    private LogDao logDao;

    @Override
    public void add(Log log) {
        if (log == null) {
            throw new ServiceException("添加失败：用户信息为空");
        }
        if (StringUtils.isEmpty(log.getUsername().trim())) {
            throw new ServiceException("添加失败：用户名为空");
        }
        if (StringUtils.isEmpty(log.getParams())){
            throw new ServiceException("添加失败：请求参数为空");
        }
        if (StringUtils.isEmpty(log.getIp())) {
            throw new ServiceException("添加失败：ip为空");
        }
        if (StringUtils.isEmpty(log.getTime().toString())) {
            throw new ServiceException("添加失败：执行时长为空");
        }
        /*添加人*/
        log.setAddUser(ShiroUtils.getPrincipal().getName());
        /*添加时间*/
        log.setAddTime(new Date());
        /*删除状态：0 正常 1 以删除*/
        log.setIsdel(0);
        logDao.add(log);
    }

   @Override
   public List<Log> getLogPageList(String criteria, Integer pageNo, Integer pageSize) {
       if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
           throw new IllegalArgumentException("参数不合法，请联系运维人员");
       }
       /*假设pageSize=10*/
       /*1 (1-1) x 10 + 1 = 1*/
       /*2 (2-1) x 10 + 1 = 11*/
       /*3 (3-1) x 10 + 1 = 21*/ /*@ming*/
       int startIndex = (pageNo - 1) * pageSize + 1;
       /*1 1 + 10 - 1 = 10*/
       /*2 11 + 10 - 1 = 20*/
       /*3 21 + 10 - 1 = 30*/ /*@ming*/
       int endIndex = startIndex + pageSize - 1;
       List<Log> list = logDao.getLogPageList(criteria, startIndex, endIndex);
       if (list == null) {
           throw new ServiceException("没有查询到数据");
       }
       return list;
   }

    @Override
    public List<Log> getLogPageList(String criteria, String startTime, String endTime, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
            throw new IllegalArgumentException("参数不合法，请联系运维人员");
        }
        /*假设pageSize=10*/
        /*1 (1-1) x 10 + 1 = 1*/
        /*2 (2-1) x 10 + 1 = 11*/
        /*3 (3-1) x 10 + 1 = 21*/ /*@ming*/
        int startIndex = (pageNo - 1) * pageSize + 1;
        /*1 1 + 10 - 1 = 10*/
        /*2 11 + 10 - 1 = 20*/
        /*3 21 + 10 - 1 = 30*/ /*@ming*/
        int endIndex = startIndex + pageSize - 1;
        List<Log> list = logDao.getLogPageListByTime(criteria, startTime, endTime, startIndex, endIndex);
        if (list == null) {
            throw new ServiceException("没有查询到数据");
        }
        return list;
    }

}
